package cn.wangxl.loglog.service;

import cn.wangxl.loglog.constants.LogTypeEnum;
import cn.wangxl.loglog.service.bo.BasicLogBO;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Description TODO
 * @Classname RocketmqService
 * @Created by wangxl
 * @Date 2020/5/10 23:17
 */
@Service
@Slf4j
public class RocketmqService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    public void saveAsync(BasicLogBO basicLogBO, LogTypeEnum logTypeEnum) {

        Objects.requireNonNull(logTypeEnum);
        rocketMQTemplate.asyncSend(logTypeEnum.getTopic(), basicLogBO, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("入库成功");
            }

            @Override
            public void onException(Throwable e) {
                System.out.println(e.getCause());
                System.out.println(e.getMessage());
                System.out.println("入库失败");
            }
        });
    }

    public void saveSync(BasicLogBO basicLogBO, LogTypeEnum logTypeEnum) {

        Objects.requireNonNull(logTypeEnum);
        rocketMQTemplate.convertAndSend(logTypeEnum.getTopic(), basicLogBO);

    }

    public void saveOneway(BasicLogBO basicLogBO, LogTypeEnum logTypeEnum) {

        Objects.requireNonNull(logTypeEnum);
        rocketMQTemplate.sendOneWay(logTypeEnum.getTopic(), basicLogBO);

    }

}
